-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpeg2rb - User Guide.html
More file actions
687 lines (687 loc) · 27.2 KB
/
peg2rb - User Guide.html
File metadata and controls
687 lines (687 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
<TITLE></TITLE>
<META NAME="GENERATOR" CONTENT="OpenOffice 4.0.0 (Win32)">
<META NAME="CREATED" CONTENT="20130826;18542682">
<META NAME="CHANGED" CONTENT="20140717;16502084">
<STYLE TYPE="text/css">
<!--
@page { margin: 2cm }
H2.western { font-family: "Arial", sans-serif; font-size: 14pt; font-style: italic }
H2.cjk { font-family: "Arial Unicode MS"; font-size: 14pt; font-style: italic }
H2.ctl { font-family: "Mangal"; font-size: 14pt; font-style: italic }
P.sdfootnote { font-size: 10pt }
H3.western { font-family: "Arial", sans-serif }
H3.cjk { font-family: "Arial Unicode MS" }
H3.ctl { font-family: "Mangal" }
H4.western { font-family: "Arial", sans-serif; font-size: 11pt; font-style: italic }
H4.cjk { font-family: "Arial Unicode MS"; font-size: 11pt; font-style: italic }
H4.ctl { font-family: "Mangal"; font-size: 11pt; font-style: italic }
H5.western { font-family: "Arial", sans-serif; font-size: 11pt }
H5.cjk { font-family: "Arial Unicode MS"; font-size: 11pt }
H5.ctl { font-family: "Mangal"; font-size: 11pt }
CODE { font-family: "Courier New", monospace }
A.sdfootnoteanc { font-size: 57% }
-->
</STYLE>
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<H2 CLASS="western">Input grammar</H2>
<P>Input grammar consists of:</P>
<UL>
<LI><P>Rules (at least one)</P>
<LI><P>Code insertions (optional)</P>
<LI><P>Comments (optional)</P>
</UL>
<H3 CLASS="western">Rules</H3>
<P>Rules have the following form:</P>
<P><CODE><SPAN LANG="en-US">nonterminal <- expression ;</SPAN></CODE></P>
<P><SPAN LANG="en-US">(You may use “</SPAN><CODE><SPAN LANG="en-US">=</SPAN></CODE>”
<SPAN LANG="en-US">or “</SPAN><CODE>←</CODE>” <SPAN LANG="en-US">instead
of “</SPAN><CODE><SPAN LANG="en-US"><-</SPAN></CODE>”<SPAN LANG="en-US">.)</SPAN></P>
<P><CODE><SPAN LANG="en-US">expression</SPAN></CODE> <SPAN LANG="en-US">describes
how to parse input. Valid </SPAN><CODE><SPAN LANG="en-US">expression</SPAN></CODE><SPAN LANG="en-US">s
are listed in “Parsing expressions” section below.</SPAN></P>
<H4 CLASS="western">Semantic values</H4>
<P><SPAN LANG="en-US">Each rule has a semantic value. You may set it
in runtime by setting Ruby variable </SPAN><CODE><SPAN LANG="en-US">val</SPAN></CODE>
<SPAN LANG="en-US">in the rule's right-hand side expression. When the
rule is executed, its semantic value becomes semantic value of the
nonterminal which caused the rule execution. In the following example
nonterminal </SPAN><CODE><SPAN LANG="en-US">ntrm</SPAN></CODE> <SPAN LANG="en-US">in
the first rule receives semantic value </SPAN><CODE><SPAN LANG="en-US">"b"</SPAN></CODE><SPAN LANG="en-US">:</SPAN></P>
<P><CODE><SPAN LANG="en-US">text ← "a" ntrm ;</SPAN></CODE></P>
<P><CODE><SPAN LANG="en-US">ntrm ← "b":val ;</SPAN></CODE></P>
<P>You may capture the semantic values into Ruby variables (as shown
in the example; see corresponding “Capture” expressions in
“Parsing expressions” section), process them inside code
insertions (the semantic values are valid Ruby objects) and even
return them as resultant values from the parser.</P>
<P>You also may define the type of the semantic value of the rule:</P>
<P><CODE><SPAN LANG="en-US">nonterminal: Type ← expression ;</SPAN></CODE></P>
<P>(Currently semantic value types are not processed anyhow and serve
informational purposes only.)</P>
<H4 CLASS="western">Parsing expressions</H4>
<TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000000" CELLPADDING=5 CELLSPACING=0>
<COL WIDTH=45*>
<COL WIDTH=11*>
<COL WIDTH=68*>
<COL WIDTH=40*>
<COL WIDTH=54*>
<COL WIDTH=39*>
<THEAD>
<TR VALIGN=TOP>
<TH COLSPAN=3 WIDTH=48%>
<P>Expression</P>
</TH>
<TH COLSPAN=2 WIDTH=37%>
<P><FONT FACE="Times New Roman, serif">Meaning</FONT></P>
</TH>
<TH WIDTH=15%>
<P>Semantic value</P>
</TH>
</TR>
</THEAD>
<TBODY>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">"string"</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 COLSPAN=2 WIDTH=37%>
<P><FONT FACE="Times New Roman, serif"><SPAN LANG="en-US">Read
</SPAN></FONT><CODE><SPAN LANG="en-US">string</SPAN></CODE> <FONT FACE="Times New Roman, serif"><SPAN LANG="en-US">from
the input.</SPAN></FONT></P>
</TD>
<TD ROWSPAN=2 WIDTH=15%>
<P><CODE><SPAN LANG="en-US">string<A CLASS="sdfootnoteanc" NAME="sdfootnote1anc" HREF="#sdfootnote1sym"><SUP>1</SUP></A></SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">'string'</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">U+HHHH</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P>Read UTF-8 encoded character with Unicode code U+HHHH from the
input.</P>
</TD>
<TD WIDTH=15%>
<P><SPAN LANG="en-US">A </SPAN><CODE><SPAN LANG="en-US">String</SPAN></CODE>
<SPAN LANG="en-US">containing the read character.</SPAN></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">char</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P>Read UTF-8 encoded character from the input.</P>
</TD>
<TD WIDTH=15%>
<P><SPAN LANG="en-US">A </SPAN><CODE><SPAN LANG="en-US">String</SPAN></CODE>
<SPAN LANG="en-US">containing the read character.</SPAN></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">x...y</SPAN></CODE></P>
</TD>
<TD ROWSPAN=4 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US">Read a character from the input which is in
the range between </SPAN><CODE><SPAN LANG="en-US">x</SPAN></CODE>
<SPAN LANG="en-US">and </SPAN><CODE><SPAN LANG="en-US">y</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
<P><CODE><SPAN LANG="en-US">x</SPAN></CODE> <SPAN LANG="en-US">and
</SPAN><CODE><SPAN LANG="en-US">y</SPAN></CODE> <SPAN LANG="en-US">may
be of the form of </SPAN><CODE><SPAN LANG="en-US">"c"</SPAN></CODE><SPAN LANG="en-US">,
</SPAN><CODE><SPAN LANG="en-US">'c'</SPAN></CODE> <SPAN LANG="en-US">or
</SPAN><CODE><SPAN LANG="en-US">U+HHHH</SPAN></CODE><SPAN LANG="en-US">.
Examples:</SPAN></P>
<UL>
<LI><P><CODE><SPAN LANG="en-US">'a'...'z'</SPAN></CODE></P>
<LI><P><CODE><SPAN LANG="en-US">U+0000...U+0020</SPAN></CODE></P>
<LI><P><CODE><SPAN LANG="en-US">'a'...U+2025</SPAN></CODE></P>
</UL>
</TD>
<TD ROWSPAN=4 WIDTH=15%>
<P><SPAN LANG="en-US">A </SPAN><CODE><SPAN LANG="en-US">String</SPAN></CODE>
<SPAN LANG="en-US">containing the read character.</SPAN></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">x..y</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">x…</SPAN></CODE><CODE><FONT FACE="Courier New, monospace"><SPAN LANG="en-US">y</SPAN></FONT></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">x..y<A CLASS="sdfootnoteanc" NAME="sdfootnote2anc" HREF="#sdfootnote2sym"><SUP>2</SUP></A></SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">{ code }</SPAN></CODE></P>
</TD>
<TD ROWSPAN=3 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US">Execute </SPAN><CODE><SPAN LANG="en-US">code</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
<P><STRONG><SPAN LANG="en-US">Attention!</SPAN></STRONG> <SPAN LANG="en-US">Do
not declare variables or use </SPAN><CODE><SPAN LANG="en-US">Symbol</SPAN></CODE><SPAN LANG="en-US">s
with names starting with “</SPAN><CODE><SPAN LANG="en-US">yy_</SPAN></CODE>”
<SPAN LANG="en-US">in </SPAN><CODE><SPAN LANG="en-US">code</SPAN></CODE><SPAN LANG="en-US">!
They are reserved by the parser generator!</SPAN></P>
</TD>
<TD ROWSPAN=3 WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">{... code ...}</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">{... code }...</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">$</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P>Fail everywhere except the end of the input.</P>
</TD>
<TD WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">^</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P>Fail everywhere except the beginning of the input.</P>
</TD>
<TD WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">@</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P>Get current position in input.</P>
</TD>
<TD WIDTH=15%>
<P>Current position in input.</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=17%>
<P><CODE><SPAN LANG="en-US">@=var</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=31%>
<P><CODE><SPAN LANG="en-US">@=var:</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US">Set current position in input to the
position stored in Ruby variable </SPAN><CODE><SPAN LANG="en-US">var</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
</TD>
<TD ROWSPAN=2 WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=17%>
<P><CODE><SPAN LANG="en-US">at var</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=31%>
<P><CODE><SPAN LANG="en-US">at var:</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">nonterminal</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US">Use the rule having </SPAN><CODE><SPAN LANG="en-US">nonterminal</SPAN></CODE>
<SPAN LANG="en-US">as its left part to parse input.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P>The rule's semantic value.</P>
</TD>
</TR>
<TR>
<TD COLSPAN=6 WIDTH=100% VALIGN=TOP>
<P ALIGN=CENTER STYLE="widows: 4"><I><B>Combinations (ordered by
priority)<A CLASS="sdfootnoteanc" NAME="sdfootnote3anc" HREF="#sdfootnote3sym"><SUP>3</SUP></A></B></I></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">expr1 / expr2</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Choice (ordered)</B></SPAN><SPAN LANG="en-US">.
Parse input with </SPAN><CODE><SPAN LANG="en-US">expr1</SPAN></CODE><SPAN LANG="en-US">.
If it fails then parse it with </SPAN><CODE><SPAN LANG="en-US">expr2</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
</TD>
<TD ROWSPAN=2 WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">/ expr1 / expr2</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">expr1 expr2</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Sequence</B></SPAN><SPAN LANG="en-US">.
Parse input with </SPAN><CODE><SPAN LANG="en-US">expr1</SPAN></CODE>
<SPAN LANG="en-US">then with </SPAN><CODE><SPAN LANG="en-US">expr2</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">expr ]</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US">The same as </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 WIDTH=15%>
<P><CODE><SPAN LANG="en-US">expr</SPAN></CODE><SPAN LANG="en-US">'s
semantic value.</SPAN></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">(expr)</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=17%>
<P><CODE><SPAN LANG="en-US">expr:var</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=31%>
<P><CODE><SPAN LANG="en-US">expr:(var)</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Capture (semantic value)</B></SPAN><SPAN LANG="en-US">.
Parse input with </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">and capture its semantic value into Ruby
variable </SPAN><CODE><SPAN LANG="en-US">var</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P><CODE><SPAN LANG="en-US">expr</SPAN></CODE><SPAN LANG="en-US">'s
semantic value.</SPAN></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=17%>
<P><CODE><SPAN LANG="en-US">expr :+ var</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=31%>
<P><CODE><SPAN LANG="en-US">expr :+ (var)</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Append (semantic value)</B></SPAN><SPAN LANG="en-US">.
Parse input with </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">and append its semantic value to Ruby variable
</SPAN><CODE><SPAN LANG="en-US">var</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
<P><SPAN LANG="en-US">Appending in the generated parser is
performed with Ruby operator “</SPAN><CODE><SPAN LANG="en-US"><<</SPAN></CODE>”
<SPAN LANG="en-US">(e. g., “</SPAN><CODE><SPAN LANG="en-US">var << yy_expr_value</SPAN></CODE>”<SPAN LANG="en-US">).</SPAN></P>
</TD>
<TD ROWSPAN=2 WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=17%>
<P><CODE><SPAN LANG="en-US">expr :>> var</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=31%>
<P><CODE><SPAN LANG="en-US">expr :>> (var)</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">&expr</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Predicate</B></SPAN><SPAN LANG="en-US">.
Remember current position in input, parse input with </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">and go back to the remembered position. It
fails if </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">fails.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">!expr</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Predicate (negative)</B></SPAN><SPAN LANG="en-US">.
The same as </SPAN><CODE><SPAN LANG="en-US">&expr</SPAN></CODE>
<SPAN LANG="en-US">but it fails if </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">parses input successfully.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">&{ code }</SPAN></CODE></P>
</TD>
<TD ROWSPAN=3 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Predicate (code)</B></SPAN><SPAN LANG="en-US">.
Fail if </SPAN><CODE><SPAN LANG="en-US">code</SPAN></CODE>
<SPAN LANG="en-US">evaluates to false or nil.</SPAN></P>
</TD>
<TD ROWSPAN=3 WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">&{... code ...}</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">&{... code }...</SPAN></CODE></P>
</TD>
</TR>
<TR>
<TD COLSPAN=3 WIDTH=48% VALIGN=TOP>
<P><CODE><SPAN LANG="en-US">expr*</SPAN></CODE></P>
</TD>
<TD ROWSPAN=3 WIDTH=16%>
<P><SPAN LANG="en-US"><B>Repetition</B></SPAN><SPAN LANG="en-US">.
Repeat parsing the input with </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>…</P>
</TD>
<TD WIDTH=21% VALIGN=TOP>
<P>…zero or more times.</P>
</TD>
<TD ROWSPAN=3 WIDTH=15% VALIGN=TOP>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR>
<TD COLSPAN=3 WIDTH=48% VALIGN=TOP>
<P><CODE><SPAN LANG="en-US">expr+</SPAN></CODE></P>
</TD>
<TD WIDTH=21% VALIGN=TOP>
<P>…one or more times.</P>
</TD>
</TR>
<TR>
<TD COLSPAN=3 WIDTH=48% VALIGN=TOP>
<P><CODE><SPAN LANG="en-US">expr?</SPAN></CODE></P>
</TD>
<TD WIDTH=21% VALIGN=TOP>
<P>…zero or one time.</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=3 WIDTH=48%>
<P><CODE><SPAN LANG="en-US">expr*?</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Repetition (lazy)</B></SPAN><SPAN LANG="en-US">.
Repeat parsing the input with </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">until the next expression in the sequence with
it succeeds. See examples in section “Lazy repetition:
examples”.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=2 WIDTH=22%>
<P><CODE><SPAN LANG="en-US">< expr >:var</SPAN></CODE></P>
</TD>
<TD WIDTH=27%>
<P><CODE><SPAN LANG="en-US">< expr >:(var)</SPAN></CODE></P>
</TD>
<TD COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Capture (text)</B></SPAN><SPAN LANG="en-US">.
Capture text consumed by </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">into Ruby variable </SPAN><CODE><SPAN LANG="en-US">var</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
</TD>
<TD WIDTH=15%>
<P><SPAN LANG="en-US">Captured text (as </SPAN><CODE><SPAN LANG="en-US">String</SPAN></CODE><SPAN LANG="en-US">).</SPAN></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=2 WIDTH=22%>
<P><CODE><SPAN LANG="en-US">< expr > :+ var</SPAN></CODE></P>
</TD>
<TD WIDTH=27%>
<P><CODE><SPAN LANG="en-US">< expr > :+ (var)</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 COLSPAN=2 WIDTH=37%>
<P><SPAN LANG="en-US"><B>Append (text)</B></SPAN><SPAN LANG="en-US">.
Append text consumed by </SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE>
<SPAN LANG="en-US">to Ruby variable </SPAN><CODE><SPAN LANG="en-US">var</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
<P><SPAN LANG="en-US">Appending in the generated parser is
performed with Ruby operator “</SPAN><CODE><SPAN LANG="en-US"><<</SPAN></CODE>”<SPAN LANG="en-US">.</SPAN></P>
</TD>
<TD ROWSPAN=2 WIDTH=15%>
<P ALIGN=CENTER STYLE="widows: 4">—</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=2 WIDTH=22%>
<P><CODE><SPAN LANG="en-US">< expr > :>> var</SPAN></CODE></P>
</TD>
<TD WIDTH=27%>
<P><CODE><SPAN LANG="en-US">< expr > :>> (var)</SPAN></CODE></P>
</TD>
</TR>
</TBODY>
</TABLE>
<P><BR><BR>
</P>
<H5 CLASS="western">Lazy repetition: examples</H5>
<TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000000" CELLPADDING=5 CELLSPACING=0>
<COL WIDTH=128*>
<COL WIDTH=128*>
<THEAD>
<TR VALIGN=TOP>
<TH WIDTH=50%>
<P>Expression</P>
</TH>
<TH WIDTH=50%>
<P>Equivalent to…</P>
</TH>
</TR>
</THEAD>
<TBODY>
<TR VALIGN=TOP>
<TD WIDTH=50%>
<P><CODE><SPAN LANG="en-US">a*? b</SPAN></CODE></P>
</TD>
<TD WIDTH=50%>
<P><CODE><SPAN LANG="en-US">(!b a) b</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=50%>
<P><CODE><SPAN LANG="en-US">(a*?) b</SPAN></CODE></P>
</TD>
<TD WIDTH=50%>
<P><CODE><SPAN LANG="en-US">(!b a) b</SPAN></CODE></P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD WIDTH=50%>
<P><CODE><SPAN LANG="en-US">a*? b c</SPAN></CODE></P>
</TD>
<TD WIDTH=50%>
<P><CODE><SPAN LANG="en-US">(!(b c) a) b c</SPAN></CODE></P>
</TD>
</TR>
<TR>
<TD WIDTH=50% VALIGN=TOP>
<P><CODE><SPAN LANG="en-US">(a*? b c) d</SPAN></CODE></P>
</TD>
<TD ROWSPAN=2 WIDTH=50%>
<P><CODE><SPAN LANG="en-US">( (!(b c) a) b c ) d</SPAN></CODE></P>
</TD>
</TR>
<TR>
<TD WIDTH=50% VALIGN=TOP>
<P><CODE><SPAN LANG="en-US">a*? b c ] d</SPAN></CODE></P>
</TD>
</TR>
</TBODY>
</TABLE>
<P><BR><BR>
</P>
<H3 CLASS="western">Code insertions</H3>
<P>Every code insertion has one of the following forms:</P>
<UL>
<LI><P><CODE><SPAN LANG="en-US">{ code }</SPAN></CODE></P>
<LI><P><CODE><SPAN LANG="en-US">{... code ...}<A CLASS="sdfootnoteanc" NAME="sdfootnote4anc" HREF="#sdfootnote4sym"><SUP>4</SUP></A></SPAN></CODE></P>
<LI><P><CODE><SPAN LANG="en-US">{... code }...<A CLASS="sdfootnoteanc" NAME="sdfootnote5anc" HREF="#sdfootnote5sym"><SUP>5</SUP></A></SPAN></CODE></P>
</UL>
<P>They are written into the output parser “as is”. A code
insertion before all rules and other code insertions is put into the
beginning of the output file. A code insertion after all rules and
other code insertions is put into the end of the output file. Other
code insertions are put somewhere in the middle of the output file.</P>
<P><STRONG><SPAN LANG="en-US">Attention!</SPAN></STRONG> <SPAN LANG="en-US">Do
not declare methods, classes, modules, variables etc. with names
starting with “</SPAN><CODE><SPAN LANG="en-US">yy_</SPAN></CODE>”
<SPAN LANG="en-US">or “</SPAN><CODE><SPAN LANG="en-US">YY_</SPAN></CODE>”
<SPAN LANG="en-US">in the code insertions! They are reserved by the
parser generator!</SPAN></P>
<H3 CLASS="western">Comments</H3>
<P><SPAN LANG="en-US">Comments can be written anywhere in the input
grammar except identifiers and atomic expressions. A comment starts
with “</SPAN><CODE><SPAN LANG="en-US">#</SPAN></CODE>” <SPAN LANG="en-US">or
“</SPAN><CODE><SPAN LANG="en-US">--</SPAN></CODE>” <SPAN LANG="en-US">and
continues until the end of the line.</SPAN></P>
<P>Following are examples of comments:</P>
<P><CODE><SPAN LANG="en-US"># Simple comment</SPAN></CODE></P>
<P><CODE><SPAN LANG="en-US">-- Another comment</SPAN></CODE></P>
<P><CODE><SPAN LANG="en-US">---- Section delimiter, a comment too
----</SPAN></CODE></P>
<H2 CLASS="western">Output parser</H2>
<P>The output of the parser generator is a Ruby script containing the
following:</P>
<OL>
<LI><P>Content of the code insertion before all rules and other code
insertions (if the code insertion is present);</P>
<LI><P><CODE><SPAN LANG="en-US">yy_parse(io)</SPAN></CODE> <SPAN LANG="en-US">function
definition. It receives an </SPAN><CODE><SPAN LANG="en-US">IO</SPAN></CODE>
<SPAN LANG="en-US">and parses it according to the first rule in the
input grammar. Its return value is semantic value of that rule. If
it can not parse the input (due to syntax error in it) then it
raises </SPAN><CODE><SPAN LANG="en-US">YY_SyntaxError</SPAN></CODE>
<SPAN LANG="en-US">(see below). The </SPAN><CODE><SPAN LANG="en-US">io</SPAN></CODE>
<SPAN LANG="en-US">must support following methods: </SPAN><CODE><SPAN LANG="en-US">read(…)</SPAN></CODE><SPAN LANG="en-US">,
</SPAN><CODE><SPAN LANG="en-US">pos</SPAN></CODE><SPAN LANG="en-US">,
</SPAN><CODE><SPAN LANG="en-US">pos=(…)</SPAN></CODE> <SPAN LANG="en-US">and
</SPAN><CODE><SPAN LANG="en-US">set_encoding(…)</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
<LI><P><CODE><SPAN LANG="en-US">YY_SyntaxError</SPAN></CODE> <SPAN LANG="en-US">class
definition. It is a subclass of </SPAN><CODE><SPAN LANG="en-US">Exception</SPAN></CODE>
<SPAN LANG="en-US">and has the following methods:</SPAN></P>
<OL>
<LI><P><CODE><SPAN LANG="en-US">pos</SPAN></CODE><SPAN LANG="en-US">.
It returns an offset in input (in bytes) the </SPAN><CODE><SPAN LANG="en-US">YY_SyntaxError</SPAN></CODE>
<SPAN LANG="en-US">has occurred at.</SPAN></P>
</OL>
<LI><P>Auxiliary functions and classes definitions. Their names
always start with “<CODE>yy_</CODE>” (for functions) or “<CODE>YY_</CODE>”
(for classes and modules).</P>
<LI><P>Content of the code insertion after all rules and other code
insertions (if the code insertion is present).</P>
</OL>
<H3 CLASS="western">Multiple parser entry points</H3>
<P><CODE>yy_parse(io)</CODE> always starts parsing with the first
rule from the input grammar. But what if you need to start parsing
with another rule?</P>
<P>No problem, just define the required rule as follows:</P>
<P><CODE><SPAN LANG="en-US">rule_name() ← expression ; # notice
parentheses after the rule name</SPAN></CODE></P>
<P><SPAN LANG="en-US">and the parser generator will generate a
function which is named after the rule's left-hand side nonterminal
(</SPAN><CODE><SPAN LANG="en-US">rule_name</SPAN></CODE> <SPAN LANG="en-US">in
this example) and which behaves the same as </SPAN><CODE><SPAN LANG="en-US">yy_parse(io)</SPAN></CODE>
<SPAN LANG="en-US">but starts parsing input with the defined rule.</SPAN></P>
<P>Other valid forms for such a rule are:</P>
<UL>
<LI><P><CODE><SPAN LANG="en-US">rule_name(...) ← expression
;</SPAN></CODE><SPAN LANG="en-US"><BR>(here “...” is three dots,
not some arbitrary expression)</SPAN></P>
<LI><P><CODE><SPAN LANG="en-US">rule_name(arg) ← expression
;</SPAN></CODE><SPAN LANG="en-US"><BR>(here arg is a valid Ruby
variable name)</SPAN></P>
</UL>
<P>Of course you may define the rule's semantic value type as well:</P>
<P><CODE><SPAN LANG="en-US">rule_name(): Type ← expression ;</SPAN></CODE></P>
<H4 CLASS="western">Example</H4>
<P>The following input grammar:</P>
<P><CODE><SPAN LANG="en-US">main ← rule2 ;<BR>rule2(io) ← … ;</SPAN></CODE></P>
<P>results in the following functions in the output parser:</P>
<UL>
<LI><P><CODE><SPAN LANG="en-US">yy_parse(io)</SPAN></CODE>—<SPAN LANG="en-US">it
starts parsing input with </SPAN><CODE><SPAN LANG="en-US">main</SPAN></CODE>
<SPAN LANG="en-US">rule;</SPAN></P>
<LI><P><CODE><SPAN LANG="en-US">rule2(io)</SPAN></CODE>—<SPAN LANG="en-US">it
starts parsing input with </SPAN><CODE><SPAN LANG="en-US">rule2</SPAN></CODE><SPAN LANG="en-US">.</SPAN></P>
</UL>
<DIV ID="sdfootnote1">
<P CLASS="sdfootnote"><A CLASS="sdfootnotesym" NAME="sdfootnote1sym" HREF="#sdfootnote1anc">1</A><SPAN LANG="en-US">Its
</SPAN><CODE><SPAN LANG="en-US">String#encoding</SPAN></CODE> <SPAN LANG="en-US">is
set to “</SPAN><CODE><SPAN LANG="en-US">UTF-8</SPAN></CODE>”<SPAN LANG="en-US">.</SPAN></P>
</DIV>
<DIV ID="sdfootnote2">
<P CLASS="sdfootnote"><A CLASS="sdfootnotesym" NAME="sdfootnote2sym" HREF="#sdfootnote2anc">2</A>Here
“<CODE><SPAN LANG="en-US">..</SPAN></CODE>” is Unicode character
with code U+2025.</P>
</DIV>
<DIV ID="sdfootnote3">
<P CLASS="sdfootnote"><A CLASS="sdfootnotesym" NAME="sdfootnote3sym" HREF="#sdfootnote3anc">3</A><SPAN LANG="en-US">Here
</SPAN><CODE><SPAN LANG="en-US">expr</SPAN></CODE><SPAN LANG="en-US">,
</SPAN><CODE><SPAN LANG="en-US">expr1</SPAN></CODE><SPAN LANG="en-US">,
</SPAN><CODE><SPAN LANG="en-US">expr2</SPAN></CODE><SPAN LANG="en-US">,
… are other valid expressions.</SPAN></P>
</DIV>
<DIV ID="sdfootnote4">
<P CLASS="sdfootnote"><A CLASS="sdfootnotesym" NAME="sdfootnote4sym" HREF="#sdfootnote4anc">4</A>White
space finished with newline after the first "<CODE><SPAN LANG="en-US">...</SPAN></CODE>"
is ignored.</P>
</DIV>
<DIV ID="sdfootnote5">
<P CLASS="sdfootnote"><A CLASS="sdfootnotesym" NAME="sdfootnote5sym" HREF="#sdfootnote5anc">5</A>White
space finished with newline after the first "<CODE><SPAN LANG="en-US">...</SPAN></CODE>"
is ignored.</P>
</DIV>
</BODY>
</HTML>